{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Tutorial: Batch Processing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This tutorial shows how to solve for solutions to different scenarios in a batch processing fashion. You can find all realted files in [this](https://gitlab.lrz.de/tum-cps/commonroad_io/tree/batch_processing) repository. The main components are:\n",
    "* a configuration file (.yaml format),\n",
    "* some helper functions,\n",
    "* and the script below to call the planer function.\n",
    "\n",
    "Before proceeding, you should make sure the configuration file is set correctly. Here are the explanation for each of the parameters in the configuration file:\n",
    "* **input_path**: input directory of CommonRoad scenarios that you indend to solve.\n",
    "* **output_path**: output directory of the solution files.\n",
    "* **overwrite**: flag to determine whether the existing solution files should be overwritten.\n",
    "* **timeout**: timeout time for your motion planner, unit in seconds\n",
    "* **motion_planner_path**: directory where the module containing the function to execute your motion planner is located\n",
    "* **motion_planner_module_name**: name of the module that contains the function to execute your motion planner\n",
    "* **motion_planner_function_name**: name of the function that executes your motion planner\n",
    "* **default**: parameters specified under this section will be applied to all scenarios. If you wish to specify a different paramter for specific scenarios, simply copy the section and replace 'default' with the id of your scenario.\n",
    "* **vehicle_model**: model of the vehicle, valid values: **PM, KS, ST and MB**. Please refer to [Vehicle Models](https://gitlab.lrz.de/tum-cps/commonroad-vehicle-models/blob/master/vehicleModels_commonRoad.pdf) for more information.\n",
    "* **vehicle_type**: type of the vehicle, valid values: **FORD_ESCORT, BMW_320i and VW_VANAGON**.\n",
    "* **cost_function**: identifier of cost function. Please refer to [Cost Functions](https://gitlab.lrz.de/tum-cps/commonroad-cost-functions/blob/master/costFunctions_commonRoad.pdf) for more information.\n",
    "* **planner_id**: id of the planner that is used to solve for solutions. in this example, 1 = Greedy Best First Search, 2 = A*, else = Your own planner.\n",
    "* **planning_problem_idx**: planner problem index. for cooperative scenarios: 0, 1, 2, ..., otherwise: 0\n",
    "* **max_tree_depth**: maximum permissible depth of the search tree\n",
    "\n",
    "Note: the paths can be either **relative** to this script or **absolute**.\n",
    "\n",
    "Simply run the following script to start batch processing. The results here is just exemplary, you will see different output as your configuration varies."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import helper functions\n",
    "from helper_functions import *\n",
    "\n",
    "# specify path to configuration file\n",
    "path_file_config = \"../configuration/batch_processing_config.yaml\"\n",
    "\n",
    "# load configuration file\n",
    "configuration = load_configuration(path_file_config)\n",
    "\n",
    "# get target function\n",
    "function_target = get_target_function(configuration)\n",
    "\n",
    "# get a list of scenario files\n",
    "list_files_input = get_input_files(configuration)\n",
    "\n",
    "# get length of the list and time before timeout\n",
    "num_files = len(list_files_input)\n",
    "time_timeout = configuration['timeout']\n",
    "\n",
    "print(\"Total number of files to be processed: {}\".format(num_files))\n",
    "print(\"Timeout setting: {} seconds\\n\".format(time_timeout))\n",
    "count_processed = 0\n",
    "\n",
    "# iterate through scenarios\n",
    "for file_scenario in list_files_input:\n",
    "    count_processed += 1\n",
    "    print(\"Processing file No. {} / {}. Scenario ID: {}\".format(count_processed, num_files, file_scenario[:-4]))\n",
    "    # parse the scenario file\n",
    "    result_parse = parse_scenario_file(configuration, file_scenario)\n",
    "    # execute target function\n",
    "    solution_trajectories = execute_target_function(function_target, result_parse, time_timeout)\n",
    "    # save solution file\n",
    "    save_solution(configuration, solution_trajectories, result_parse)\n",
    "    print(\"{:=^70s}\".format(''))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (cr37)",
   "language": "python",
   "name": "cr37"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
